<?php

namespace stlswm\DouyinOpenApi\pay\ecpay;

use stlswm\DouyinOpenApi\CurlHttp;
use stlswm\DouyinOpenApi\Response;

/**
 * 担保支付-支付-支付结果查询
 * https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/ecpay/pay-list/query
 */
class QueryOrder
{
    use CurlHttp;

    private static string $url     = 'https://developer.toutiao.com/api/apps/ecpay/v1/query_order';
    private static string $urlTest = 'https://open-sandbox.douyin.com/api/apps/ecpay/v1/query_order';

    public string $app_id;
    public string $out_order_no;
    public string $thirdparty_id;

    /**
     * @param  string  $salt
     * @param  array   $body       请求体
     * @param  bool    $inSandBox  是否沙盒测试环境
     * @return Response
     */
    public function send(string $salt, array &$body = [], bool $inSandBox = false): Response
    {
        $allowKeys = [
            'app_id',
            'out_order_no',
            'thirdparty_id',
        ];
        $body = [];
        foreach ($allowKeys as $key) {
            if (isset($this->$key)) {
                $body[$key] = $this->$key;
            }
        }
        $body['sign'] = Sign::sign($salt, $body);

        return self::postJson($inSandBox ? self::$urlTest : self::$url, $body);
    }

}